﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace a18_DependencyObject
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Person p;

        public MainWindow()
        {
            InitializeComponent();

            p = new Person();

            personNameTextblock.Text = p.Name;
        }

        private void GetPersonNameClick(object sender, RoutedEventArgs e)
        {
            p.Name = inputNameTextbox.Text;
            Debug.WriteLine("get name " + p.Name);
        }

        private void SetPersonNameClick(object sender, RoutedEventArgs e)
        {
            personNameTextblock.Text = p.Name;
            Debug.WriteLine("set name " + p.Name);
        }
    }

    class Person : DependencyObject
    {
        public Person():base()
        {}

        public static readonly DependencyProperty NameProperty
            = DependencyProperty.Register(
                "Name",
                typeof(string),
                typeof(Person),
                new PropertyMetadata("n/a") //默认值
                );

        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        //这是通用的DP变动通知. 要使用针对某个DP的变动callback, 可以考虑PropertyMetadata.
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
 	         base.OnPropertyChanged(e);
             Debug.WriteLine("changed from " + (string)e.OldValue + " to " + (string)e.NewValue);
        }
    }


}
